﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 宝箱管理
/// </summary>
public class TreasureBoxManager : MonoBehaviour {

    public GameObject dialogPre;//开启宝箱窗口预设体
    private GameObject dialog; //窗口对象
    public GameObject boxPre;//多开的时候，每个武器的预设体
    private GameObject boxGo;//武器对象

    public static int boxNum = 0; //宝箱的数量
    public int tempBoxNum = 0;//爆出的宝箱数量   tempBoxNum 和 CurrBoxNum 对比 当CurrBoxNum 和 tempBoxNum相等就表示 存在的宝箱已经捡完
    public int currBoxNum = 0;//当前捡的宝箱数量

    private int randWeaponId = 0;//随机武器的Id , 现在爆率不清楚，所有我先随机Id生成

    public static  List<WeaponInfo> weaponInfoList=null; //储存开启宝箱获得的武器集合

    public GameObject tempGo=null; //上一个武器对象

    public static TreasureBoxManager _instance;

    public GameObject main; //主页面
    private GameObject warehouse;//仓库页面

    public static int redBoxNum = 0; //红色宝箱数量
    public static int greedBoxNum = 0;//绿色宝箱数量
    public static int blueBoxNum = 0;//蓝色宝箱数量
    public static int pinkBoxNum = 0;//粉色宝箱数量
    public static int purpleBoxNum = 0;//紫色宝箱数量


    void Awake()
    {
        _instance = this;
    }

    void Start()
    {
        weaponInfoList = new List<WeaponInfo>();
    }

    //单独开
    public void OpenSingle()
    {
        if (boxNum == 0)
        {
            Debug.Log("没有宝箱");
            return;
        }
        if (warehouse == null)
        {
            warehouse = GameObject.Find("Warehouse");
        }
        if (dialog == null)
        {
            dialog = GameObject.Instantiate(dialogPre, warehouse.transform);
        }else
        {
            dialog.SetActive(true);
        }
        //得到单独开武器的窗口
        Transform weapon = dialog.transform.Find("single/weapon");
        //随机武器Id
        randWeaponId = Random.Range(11,30);
        //根据Id得到武器对象信息
        WeaponInfo info = DataLists.instance.FindWeaponInfoById(randWeaponId);
        //设置对应武器的Icon
        dialog.transform.Find("single/weapon/icon").GetComponent<Image>().sprite =Resources.Load(info.Icon,typeof(Sprite)) as Sprite;
        //设置武器的名字
        dialog.transform.Find("single/weaponName").GetComponent<Text>().text = info.NameChina;
        //设置武器的描述
        dialog.transform.Find("single/weaponDes").GetComponent<Text>().text = info.Desc;
        //设置武器的攻击力
        dialog.transform.Find("single/weaponPower").GetComponent<Text>().text = info.Damage+"";
        //设置武器的攻击距离
        dialog.transform.Find("single/attackDistance").GetComponent<Text>().text = info.AttackDistance+"";
        //根据等级设置背景图 ， ABCDS级背景图的颜色不一样 ,单开没有ABCDS文字表示等级，只能开背景图颜色 
        weapon.Find("bg").GetComponent<Image>().sprite = GetGradeSingle(info.Grade);
        //隐藏多开窗口
        dialog.transform.Find("multiple").gameObject.SetActive(false);
        //显示单开窗口
        dialog.transform.Find("single").gameObject.SetActive(true);
        //把宝箱添加到集合中
        weaponInfoList.Add(info);
        //宝箱数量减1 开了一个宝箱
        boxNum -= 1;
    }
    //多开
    public void OpenAll()
    {
        if (boxNum == 0)
        {
            Debug.Log("没有宝箱");
            return;
        }
        //仓库页面,宝箱窗口是warehouse的子物体
        if (warehouse == null)
        {
            warehouse = GameObject.Find("Warehouse");
        }
        //开宝箱的窗口
        if (dialog == null)
        {
            //如何不存在，实例化到 Warehouse 下
            dialog = GameObject.Instantiate(dialogPre, warehouse.transform);
        }
        else
        {
            //存在，显示
            dialog.SetActive(true);
        }
        dialog.transform.Find("multiple").gameObject.SetActive(true);
        dialog.transform.Find("single").gameObject.SetActive(false);
        for (int i = 0; i < boxNum; i++)
        {
            //随机武器Id
            randWeaponId = Random.Range(11, 30);
            //根据Id得到武器对象信息
            WeaponInfo info = DataLists.instance.FindWeaponInfoById(randWeaponId);
            //将武器实例化到表格中
            GameObject weaponIconGo = GameObject.Instantiate(boxPre, dialog.transform.Find("multiple/ScrollRectPanel/Grid"));
            //设置该武器的信息
            weaponIconGo.GetComponent<WeaponIconBag>().weaponInfo = info;
            //显示武器对应的等级
            weaponIconGo.transform.Find("grade").GetComponent<Text>().text = info.Grade;
            //显示武器对应的Icon
            weaponIconGo.transform.Find("weaponIcon").GetComponent<Image>().sprite = Resources.Load(info.Icon, typeof(Sprite)) as Sprite;
            //weaponInfoList.Add(info);
        }
        //根据内容调整高度
        AdjustHeight._instance.SetGridHeight();
    }



    //根据武器的等级来设置武器背景图片
    Sprite GetGradeSingle(string grade)
    {
        if (string.Equals(grade,"C"))
        {
            //加载背景资源 ， 单独开的时候，武器的背景图片根据等级显示
            return Resources.Load("Dialog/green_weapon",typeof(Sprite)) as Sprite;
        }
        else if (string.Equals(grade, "B"))
        {
            return Resources.Load("Dialog/blue_weapon", typeof(Sprite)) as Sprite;
        }
        else if (string.Equals(grade, "A"))
        {
            return Resources.Load("Dialog/purple_weapon", typeof(Sprite)) as Sprite;
        }
        else if (string.Equals(grade, "S"))
        {
            return Resources.Load("Dialog/golden_weapon", typeof(Sprite)) as Sprite;
        }
        return Resources.Load("Dialog/write_weapon", typeof(Sprite)) as Sprite;
    }
    //初始化数据
    public void Init()
    {
        currBoxNum = 0;
        tempBoxNum = 0;
        redBoxNum = 0;
        greedBoxNum = 0;
        blueBoxNum = 0;
        pinkBoxNum = 0;
        purpleBoxNum = 0;
    }

    //设置宝箱的数量
    public void SetBoxNum(int num)
    {
        boxNum = num;
    }
    //增加宝箱的数量
    public void CurrBoxNumAdd(int num=1)
    {
        currBoxNum += num;
        Debug.Log(currBoxNum);
    }
    //获取仓库里的宝箱数量
    public int GetBoxNum()
    {
        return boxNum;
    }
    //得到全部宝箱，有些时候玩家喜欢筹宝箱不开，一直在掠夺，这个方法就是把每局的获得的宝箱累加到未开的宝箱数量上
    public int GetAllBox()
    {
        return (currBoxNum + boxNum);
    }

    //当前已经爆了的宝箱数量+1 
    public void TempBoxNumAdd(int num = 1)
    {
        tempBoxNum += num;
    }



    public List<WeaponInfo> GetWeaponList()
    {
        return weaponInfoList;
    }
    //仓库返回主页面的按钮方法 （临时写在这里）
    public void BackBtn()
    {
        gameObject.SetActive(false);
        main.SetActive(true);
    }

    //打印本次掠夺各种宝箱的数量 ，用于调试的时候用
    public void GetBoxTypeNum()
    {
        Debug.Log("redBoxNum:"+redBoxNum+"\n"+
            "greenBoxNum:"+greedBoxNum+"\n"+
            "pinkBoxNum:" + pinkBoxNum + "\n" +
            "purpleBoxNum:" + purpleBoxNum + "\n" +
            "blueBoxNum:" + blueBoxNum );
    }
}
